// 本地存储
import storage from 'good-storage'
function inertArray(arr, val, compare, maxLen) { //
  const index = arr.findIndex(compare)
    if (index > -1) {
      return
    }
    arr.unshift(val)// 插入数组前面
    if (maxLen && arr.length > maxLen) { // 保存歌曲多少限制
      arr.pop()
    }
}
function deleteFromArray(arr, compare) {
  const index = arr.findIndex(compare)
  if (index > -1) {
    arr.splice(index, 1)
  }
}
export function save(item, key, compare, maxLen) { // 检查是否已将存在 没有存在保存到本地存储
  const items = storage.get(key, [])
  inertArray(items, item, compare)
  storage.set(key, items)
  return items
}

export function remove(key, compare) {
  const items = storage.get(key, [])
  deleteFromArray(items, compare)

  storage.set(key, items)
  return items
}

export function load(key) { // 去本地存储中查找数据
  return storage.get(key, [])
}
